#! /bin/bash

export projectpath=$GOPATH/src/gitee.com/wanttobeamaster

# exit the shell script
function exit_script(){
	exit 1
}

# clear the gridbase log
function clearlog(){
	while read -p "only clear log and data? [yes/no] " var ; do
		if [ $var = "yes" ]; then
			rm -rf /apps/gridbase/data/*
			rm -rf /apps/gridbase/log/*
			rm -rf $projectpath/gridbase/dist/crash.log
			exit_script
		fi
		if [ $var = "no" ] ; then
			rm -rf /apps/gridbase/data/*
			rm -rf /apps/gridbase/log/*
			rm -rf $projectpath/gridbase/dist/*
			break 2
		fi
	done
	
}

# jungle wether need connect network
function connectnet(){
	isconnected=$(ping baidu.com -c 2 -w 2 | grep icmp_seq)
	if [ "$isconnected" = "" ]; then
		# /home/dongda/Xiao/networkpackage/connect/dist/connect_net $1
		connect_net
	fi
}

function break_net(){
	isschool=$(ifconfig | grep inet | grep -v inet6 | grep 121.248)
	if [ "$isschool" = "" ]
	then
	exit 1
	else
#	/home/dongda/Xiao/networkpackage/breaknet/dist/breaknet $1 $2
	breaknet
	fi
}

function gitpull(){
	# go-nemo
	# cd $gopath/src/gitee.com/wanttobeamaster/go-nemo/
	cd $projectpath/go-nemo/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	
	# c-nemo
	# cd $gopath/src/gitee.com/wanttobeamaster/c-nemo/
	cd $projectpath/c-nemo/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# git checkout -b xiaoxiao
	# git checkout .
	# git pull origin xiaoxiao
	# git checkout xiaoxiao

	# gridbase
	# cd $gopath/src/gitee.com/wanttobeamaster/gridbase/
	cd $projectpath/gridbase/
	hasXiao=$(git branch -a | grep -v origin/chtholly | grep chtholly)
	if [ "$hasXiao" = "" ]; then
		git checkout -- .
		git checkout -b xiaoxiao
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# etcd
	# cd $gopath/src/gitee.com/wanttobeamaster/etcd/
	cd $projectpath/etcd/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# pkg
	# cd $gopath/src/gitee.com/wanttobeamaster/pkg/
	cd $projectpath/pkg/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# go-systemd
	# cd $gopath/src/gitee.com/wanttobeamaster/go-systemd
	cd $projectpath/go-systemd/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# go-semver
	# cd $gopath/src/gitee.com/wanttobeamaster/go-semver
	cd $projectpath/go-semver/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	# bbolt
	# cd $gopath/src/gitee.com/wanttobeamaster/bbolt
	cd $projectpath/bbolt/
	hasXiao=$(git branch -a | grep -v origin/xiaoxiao | grep xiaoxiao)
	if [ "$hasXiao" = "" ]; then
		git checkout -b xiaoxiao
		git checkout .
		git pull origin xiaoxiao
	else
		git checkout xiaoxiao
		git pull origin xiaoxiao
	fi
	
	cd $projectpath/ela-up/
	git checkout master
	git pull origin master
}

function gitclone(){
	git clone https://gitee.com/wanttobeamaster/gridbase $projectpath/gridbase/
	git clone https://gitee.com/wanttobeamaster/go-nemo $projectpath/go-nemo/
	git clone https://gitee.com/wanttobeamaster/c-nemo $projectpath/c-nemo/
	git clone https://gitee.com/wanttobeamaster/rocksdb $projectpath/c-nemo/internal/3rdparty/nemo-rocksdb/rocksdb/
	git clone https://gitee.com/wanttobeamaster/etcd $projectpath/etcd/
	git clone https://gitee.com/wanttobeamaster/pkg $projectpath/pkg/
	git clone https://gitee.com/wanttobeamaster/bbolt $projectpath/bbolt/
	git clone https://gitee.com/wanttobeamaster/go-systemd $projectpath/go-systemd/
	git clone https://gitee.com/wanttobeamaster/go-semver $projectpath/go-semver/
	git clone https://gitee.com/wanttobeamaster/ela-up $projectpath/ela-up/
}
function pmake(){
	while read -p "whether need MAKE elasticel? [yes/no] " answer ; do
		if [ $answer = "yes" ]; then 
			cd $projectpath/gridbase/
			make
			cd ./dist
			break
		else if [ $answer = "no" ]; then
			echo "don't make MAKEFILE"
			break
		fi
		fi
	done
}


## centos setup golang
function centos_setup_golang(){
	echo " "
	echo " "
	echo " enter centos setup golang "
	mkdir /home
	mkdir /home/GoFile
	cd /home
	yum -y install bzip2
	wget https://storage.googleapis.com/golang/go1.16.3.linux-amd64.tar.gz
	tar -xf go1.16.3.linux-amd64.tar.gz
	
	echo "export GOROOT=/home/go" >> ~/.bashrc
	echo "export GOPATH=/home/GoFile" >> ~/.bashrc
	echo "export PATH=\$GOPATH/BIN:\$GOROOT/bin:\$PATH" >> ~/.bashrc
	echo "export GOPROXY=https://goproxy.io,direct" >> ~/.bashrc
	source ~/.bashrc

	echo "export GOROOT=/home/go" >> /etc/profile
	echo "export GOPATH=/home/GoFile" >> /etc/profile
	echo "export PATH=\$GOPATH/BIN:\$GOROOT/bin:\$PATH" >> /etc/profile
	echo "export GOPROXY=https://goproxy.io,direct" >> /etc/profile
	source /etc/profile

	mkdir /home/GoFile/src
	mkdir /home/GoFile/pkg
	mkdir /home/GoFile/bin

	yum -y update
	yum -y install gcc
	yum -y install glibc-static.x86_64
	yum -y install gflags-devel
	yum -y install snappy-devel
	yum -y install zlib-devel
	yum -y install bzip2-devel  
	yum -y install libstdc++-devel
	yum -y install lz4-devel
	yum -y install libzstd-devel

	cd /home
	# wget https://github.com/jemalloc/jemalloc/releases/download/5.0.1/jemalloc-5.0.1.tar.bz2
	wget https://gitee.com/wanttobeamaster/jemalloc/repository/archive/5.0.1?format=tar.gz -O jemalloc-5.0.1.tar.gz
	yum -y install bzip2
	tar -xf jemalloc-5.0.1.tar.gz
	cd jemalloc
	yum -y install autogen autoconf
	
	./autogen.sh
	make -j2
	make install
	ldconfig
	cd ../
	rm -rf jemalloc* jemalloc-5.0.1.tar.gz '5.0.1*'
	rm -rf ~/.cache/go-build

	echo "/usr/local/lib" /etc/ld.so.conf.d/usr-libs.conf
	sudo ldconfig

}


## ubuntu setup golang
function ubuntu_setup_golang(){
	mkdir /home
	mkdir /home/GoFile
	cd /home
	apt-get -y install bzip2
	curl https://storage.googleapis.com/golang/go1.16.3.linux-amd64.tar.gz --output go1.16.3.linux-amd64.tar.gz
	tar -xf go1.16.3.linux-amd64.tar.gz
	
	echo "export GOROOT=/home/go" >> ~/.bashrc
	echo "export GOPATH=/home/GoFile" >> ~/.bashrc
	echo "export PATH=\$GOPATH/BIN:\$GOROOT/bin:\$PATH" >> ~/.bashrc
	echo "export GOPROXY=https://goproxy.io,direct" >> ~/.bashrc
	source ~/.bashrc

	echo "export GOROOT=/home/go" >> /etc/profile
	echo "export GOPATH=/home/GoFile" >> /etc/profile
	echo "export PATH=\$GOPATH/BIN:\$GOROOT/bin:\$PATH" >> /etc/profile
	echo "export GOPROXY=https://goproxy.io,direct" >> /etc/profile
	source /etc/profile

	mkdir /home/GoFile/src
	mkdir /home/GoFile/pkg
	mkdir /home/GoFile/bin

	apt-get -y update
	apt-get -y install gcc
	apt-get -y install build-essential
	apt-get -y install libgflags-dev
	apt-get -y install libsnappy-dev
	apt-get -y install zlib1g-dev
	apt-get -y install libbz2-dev
	apt-get -y install liblz4-dev
	apt-get -y install libzstd-dev
	# no useful
	apt-get -y install glibc-static.x86_64
	apt-get -y install gflags-devel
	apt-get -y install snappy-devel
	apt-get -y install zlib-devel
	apt-get -y install bzip2-devel  
	apt-get -y install libstdc++-devel
	apt-get -y install lz4-devel
	apt-get -y install libzstd-devel

	cd /home
	https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0.tar.gz --output cmake-3.17.0.tar.gz
	tar -zxvf cmake-3.17.0.tar.gz
	cd cmake
	./bootstrap
	make -j8
	make install
	
	cd /home
	curl https://gitee.com/wanttobeamaster/jemalloc/repository/archive/5.0.1?format=tar.gz --output jemalloc-5.0.1.tar.gz
	tar -xf jemalloc-5.0.1.tar.gz
	cd /home/jemalloc
	apt-get -y install autogen autoconf
	./autogen.sh
	make -j2
	make install
	ldconfig
	cd ../
	rm -rf jemalloc* jemalloc-5.0.1.tar.gz
	rm -rf ~/.cache/go-build
}

function setupgolang() {
	echo "setup golang"
	if [ "$systemtype" = "centos" ] ; then 
		centos_setup_golang
	else
		ubuntu_setup_golang
	fi
}



####  START ####

git config --global  user.name wanttobeamaster
git config --global user.email 943659629@qq.com

## check ubuntu or centos
if [ -e /etc/redhat-release ]; then 
	systemtype="centos"
	echo "centos system"
fi
if [ -e /etc/lsb-release ] ; then
	systemtype="ubuntu"
	echo "ubuntu system"
fi

#### EXEC AREA

echo "  "
echo "  "
echo ======== $1 ========
echo "  "
echo "  "

echo "INFO ====== IP AND MAC"
IP=$(LC_ALL=C ifconfig | grep 'inet '| grep -v '127.0.0.1' | grep -v 172.17.0.1  | cut -d" " -f10)
# MAC=$(LC_ALL=C ifconfig | grep ether | head -n 1 | cut -d" " -f10)
MAC=$(LC_ALL=C ifconfig | grep ether | grep -v 172.17. | grep -v "txqueuelen 0" | head -n 1 | cut -d" " -f10 | sed 's/[:]//g')
echo $IP
echo $MAC

# clear ela log
echo "INFO ====== CLEAR LOG AND DATA"
clearlog


# connect network (seu)
echo " "
echo "INFO ====== CONNECT NETWORK"
#/home/dongda/Xiao/networkpackage/connect/dist/connect_net $IP
connectnet $IP

# check go env
# gopath=$GOPATH
haveGo=$(go env | grep GOPATH)
echo "check golang env"
if [[ "$haveGo" == "" ]] && [[ ! -d $GOPATH ]]; then
	echo "error ! please confirm whether setup golang"
	setupgolang
	mkdir /apps
	mkdir /apps/gridbase
	mkdir /apps/gridbase/log
	mkdir /apps/gridbase/data
fi


# git ela-up
# if [ ! -d /home/GoFile/src/gitee.com/wanttobeamaster/ela-up/ ] ; then exit 1; fi
# cd /home/GoFile/src/gitee.com/wanttobeamaster/ela-up
# git pull origin master


# git clone
echo " "
echo "INFO ====== GIT CLONE"
gitclone


# git pull
echo " "
echo "INFO ====== GIT PULL"
gitpull

# # make
# echo " "
# echo "INFO ====== MAKE"
# pmake

# break network (seu)
echo " "
echo "INFO ====== BREAK NETWORK"
break_net $IP $MAC
